// challeng/pages/tzfail/tzfail.js
var app = getApp();
function checkorder(orderid,that){
	app.util.request({
		url: 'entry/wxapp/checkorder',
		data: {
			m: 'junsion_challeng',
			orderid:orderid
		},
		cachetime: 0,
		success: function (res) {
			var data = res.data;
			if(data == 1){
				that.setData({
					openChance: !that.data.openChance
				});
				wx.showModal({
					title:'支付成功',
					showCancel:false,
					success: function(res) {
						wx.redirectTo({
		  	    			url: '../index/index'
		  	    		})
					}
				});
			}else{
				setTimeout(function(){
					checkorder(orderid,that);
				},1000);
			}
		},
	})
}
function checkRewardOrder(that){
	app.util.request({
        url: 'entry/wxapp/CheckReward',
        data: {
            m: 'junsion_challeng',
            t: 1,
            orderid:that.data.rid
        },
        cachetime: 0,
        success: function (res) {
        	  var data = res.data;
        	  if(data == 1){
    		  that.setData({
    			  showReward: !that.data.showReward
    	      });
              wx.showModal({
                title:'支付成功',
                showCancel:false,
                success: function(res) {
                	  wx.showLoading({title:'数据加载中...'});
	              	  wx.redirectTo({
	  	    			  url: '../gift/gift'
	  	    		  })
                  }
              });
            }else{
              setTimeout(function(){
            	  checkRewardOrder(that);
              },1000);
            }
         },
    })
}
Page({
	onShareAppMessage:function(){
	  var that = this;
	  var mid = that.data.mid;
	  var sharedata = that.data.shareData;
	  sharedata.path = 'challeng/pages/index/index?mid='+mid+'?gType='+that.data.game;
	  return {
		  title: sharedata.title,
	      desc: sharedata.desc,
	      path: sharedata.path,
	      imageUrl:sharedata.pic,
	      success(res){
	    	  var shareTicket = res.shareTickets[0] // 获取 shareTicket
	    	  wx.getShareInfo({
	              shareTicket: shareTicket,
	              complete(res) {
	                console.log(res) // 输出加密后的 openGId 信息
	                app.util.request({
		    	        url: 'entry/wxapp/addnums',
		    	        data: {
		    	            m : 'junsion_challeng',
		    	            t : 1,
		    	            mid : that.data.openid,
		    	            iv : res.iv,
		    	            encryptedData : res.encryptedData,
		    	        },
		    	        success: function (res) {
		    	        	if(res.data.code == 1){
		    	        		var mem = that.data.mem;
		    	        		mem.challeng_num = 1;
		                        that.setData({ mem: mem,openChance:false});
		                        wx.showToast({
	                        		title:'次数\t+'+res.data.num,
	                        		icon:'none',
	                				mask:true,
	                				duration:1000
	                			});
		    	    	    }
		    	        	else{
		    	        		 wx.showModal({title:res.data.msg,showCancel:false});
		    	        	}
		    	        }
		    	    }); 
	              }
	          });
	      },
	      fail: function(res) {
	        // 转发失败
	    	  console.log(res);
	      }
	  }
  },
  data: {
	  mem:{},
	  price:{},
	  openid:'',
	  get_share_num:0,
	  share_num:0,
	  uname:'',
      mobile:'',
      addr:'',
      remark:'',
	  express_fee:0,
	  rid:0,
	  game:0,
  },
  onLoad: function (options) {
	  var that = this;
	  var rid = '';
  	  if(options.rid){
  		rid = decodeURIComponent(options.rid);
  		that.setData({rid:rid});
      }
	  var gametype = 0;
	  var sysType = wx.getStorageSync('gameType');
	  if(sysType){
		gametype = sysType;
		that.setData({game:gametype});
	  }
	  wx.showLoading({title:'数据加载中...'});
	  app.sysInit(this,function(res){
	  	app.getUserInfo(function (res) {
    	  	var openid = res['openid'];
    	  	wx.onUserCaptureScreen(function(res) {
          	    console.log('用户截屏了')
          	    app.util.request({
      	            url: 'entry/wxapp/BlackMem',
      	            data: {
      	              m: 'junsion_challeng',
      	              openid:openid,
      	              t: 1,
      	            },
      	            success: function (res) {
      	                wx.navigateBack({});
      	            },
      	        });
          	})
    	  //获取分享内容
            app.getShareData(function(res){
            	that.setData({shareData:res.data});
            },openid,gametype);
            
    	  	that.setData({openid:openid});
	      	app.util.request({
		        url: 'entry/wxapp/tzfail',
		        data: {
		          m: 'junsion_challeng',
		          t: 1,
		          openid:openid,
		        },
		        success: function (res) {
		            wx.hideLoading();
		            that.setData({express_fee:res.data.express_fee,share_num:res.data.share_num,get_share_num:res.data.get_share_num,mem:res.data.mem,price:res.data.price});
		        }
	      	});
	  	});
	});
  },
  goBuy:function(e){
  	var that = this;
  	var dataset = e.currentTarget.dataset;
  	var selkey = dataset.index;
  	var paytype = 1;
		wx.showLoading();
		app.util.request({
          url: 'entry/wxapp/CreateOrder',
          data: {
              m : 'junsion_challeng',
              t : 1,
              openid:that.data.openid,
              paytype:paytype,
              selkey:selkey,
          },
          success: function (res) {
            wx.hideLoading();
            if(res.data.code == 1){
          	  res = res.data.res;
          	  res['success'] = function(){
          		  checkorder(res.orderid,that)
                };
                wx.requestPayment(res);
            }else{
          	  wx.showModal({title:res.data.msg,showCancel:false});
            }
          },
      });
  },
  bindName:function(e){
	  this.setData({
	    uname:e.detail.value
	  })
  },
  bindAddr:function(e){
	  this.setData({
		  addr:e.detail.value
	  })
  },
  bindMobile:function(e){
	  this.setData({
		  mobile:e.detail.value
	  })
  },
  bindRemark:function(e){
	  this.setData({
		  remark:e.detail.value
	  })
  },
  payReward:function (){
  	var that = this; 
  	var uname = that.data.uname;
  	if(!uname){
  		wx.showModal({title:'请输入联系人',showCancel:false});
  		return false;
  	}
  	var remark = that.data.remark;
  	var addr = that.data.addr;
  	if(!addr){
  		 wx.showModal({title:'请输入联系地址',showCancel:false});
  		 return false;
  	}
  	var mobile = that.data.mobile;
  	if(!mobile){
  		 wx.showModal({title:'请输入联系电话',showCancel:false});
  		 return false;
  	}
  	var regPartton=/^[1][34578]\d{9}$/; //验证手机号格式
      if(!regPartton.test(mobile)){
        wx.showModal({title:'请输入正确的11位手机号!',showCancel:false});
        return false;
      }
      var paytype = 1;
      wx.showModal({ 
  		title: '确认提交信息吗？', 
  		showCancel: true,
  		success:function(res){
  			if (res.confirm) {
  				wx.showLoading();
  				app.util.request({
  		            url: 'entry/wxapp/PayReward',
  		            data: {
  		                m : 'junsion_challeng',
  		                t : 1,
  		                openid:that.data.openid,
  		                paytype:paytype,
  		                uname : uname,
  		    	        mobile : mobile,
  		    	        addr : that.data.addr,
  		    	        remark : that.data.remark,
  		    	        id : that.data.rid,
  		            },
  		            success: function (res) {
  		              wx.hideLoading();
  		              if(res.data.code == 1){
  		            	  res = res.data.res;
  		            	  res['success'] = function(){
  		            		checkRewardOrder(that)
  		                  };
  		                  wx.requestPayment(res);
  		              }else if (res.data.code == 10){
                      wx.showModal({
                        title: '提示',
                        content: res.data.msg,
                        showCancel: false,
                        success: function (res) {
                          wx.showLoading({ title: '数据加载中...' });
                          wx.redirectTo({
                            url: '../gift/gift'
                          })
                        }
                      });
                    }else{
  		            	  wx.showModal({title:res.data.msg,showCancel:false});
  		              }
  		            },
  		        });
  			}
  		},
      });	
  },
  goAgain:function(){
	  var that = this;
	  if(that.data.game==1){
		  var url = '../splay/splay';
	  }
	  else if(that.data.game==2){
		  var url = '../tplay/tplay';
	  }
	  else if(that.data.game==3){
		  var url = '../fplay/fplay';
	  }
	  else if(that.data.game==4){
		  var url = '../wplay/wplay';
	  }
	  else if(that.data.game==5){
		  var url = '../xplay/xplay';
	  }
	  else if(that.data.game==6){
		  var url = '../cplay/cplay';
	  }
	  else if(that.data.game==7){
		  var url = '../yplay/yplay';
	  }
	  else if(that.data.game==8){
		  var url = '../qplay/qplay';
	  }
	  else if(that.data.game==9){
		  var url = '../zplay/zplay';
	  }
	  else{
		  var url = '../play/play';
	  }
	  wx.showLoading();
	  wx.redirectTo({
		  url: url
	  })
  },
  //获得更多机会
  showChance: function (e) {
      this.setData({
          openChance: !this.data.openChance
      })
  }
})